


##################################################################################################################################################################################
#
#  Martini & Walter: "Learning from Precedent" - Analysis      
#
##################################################################################################################################################################################


# Install/load required packages and set options:


options("scipen"=999, digits=4)

require(magrittr)
require(tidyverse)
require(broom) 
require(flextable)

# Note: For R and package versions used, see end of script:


# Auto-detect and set working directory: 

DIR <- strsplit(rstudioapi::getActiveDocumentContext()$path, "/")[[1]]
DIR <- paste(DIR[1:(length(DIR)-1)], collapse = "/")
setwd(DIR)


##################################################################################################################################################################################


setwd(paste0(DIR,"/Data"))
x <- read.table("LfP_analysis_data.txt", stringsAsFactors = F, header = T)
x$Date <- as.Date(x$Date)


# Table 3: DV = Demands

{
  m1 <- lm(Demands ~ 
             (scale(Ukip_vote_lag) + 
             scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m1)
  m2 <- lm(Demands ~ 
             I(scale(Ukip_vote_lag) +
             scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m2)
  m3 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m3)
  m4 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             (scale(Ukip_vote_lag) + 
             scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m4)
  m5 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             I(scale(Ukip_vote_lag) +
             scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m5)
  
  all_models <- do.call(rbind, list(
    tidy(m1) %>% mutate(model = 1),
    tidy(m2) %>% mutate(model = 2),
    tidy(m3) %>% mutate(model = 3),
    tidy(m4) %>% mutate(model = 4),
    tidy(m5) %>% mutate(model = 5)))
  
  stars <- rep("", nrow(all_models))
  stars[all_models$p.value < .1] <- "^"
  stars[all_models$p.value < .05] <- "*"
  stars[all_models$p.value < .01] <- "**"
  stars[all_models$p.value < .001] <- "***"
  
  all_models <- tibble(cbind(all_models[,1:4], stars, all_models[,5:6]))
  all_models$stars <- as.character(all_models$stars)
  all_models$stars[is.na(all_models$stars)] <- ""
  
  ols_table <- all_models %>%
    select(-statistic, -p.value) %>% 
    gather(key, value, estimate:stars) %>%
    spread(model, value) 
  
  Table <- as.data.frame(ols_table[ols_table$key != "stars",])
  Stars <- as.data.frame(ols_table[ols_table$key == "stars",])
  
  tab <- Table
  for (i in 3:ncol(tab)) tab[,i] <- as.numeric(tab[,i])
  tab[,3:ncol(tab)] <- round(tab[,3:ncol(tab)], digits = 3)
  
  Est <- tab[tab$key == "estimate",]
  SE <- tab[tab$key == "std.error",]
  
  for (r in 1:nrow(Est)) {
    for(c in 3:ncol(Est)){
      if (!is.na(Est[r,c])) Est[r,c] <- paste0(Est[r,c], Stars[r, c])          
    }}  
  for (r in 1:nrow(SE)) {
    for(c in 3:ncol(SE)){
      if (!is.na(SE[r,c])) SE[r,c] <- paste0("(", SE[r, c], ")")          
    }}  
  
  RES <- Est
  for (r in 1:nrow(RES)) {
    for(c in 3:ncol(RES)){
      if (!is.na(RES[r,c])) RES[r,c] <- paste0(RES[r, c], "\n", SE[r, c])          
    }}  
  RES <- RES[,-which(colnames(RES) == "key")]
  
  ordering <- as.data.frame(matrix(c("X1",1, "X2",1, "XR",1,"Price",2, "Ukip",3, "Tories",4, "\\+", 5, "lag_eumemb",5, 
                                     "Nat_elect",6, "EP_elect",7,"vote_share",8, "cabinet",9, 
                                     "GDP_growth",10, "Unemployment",10, "rel_gdp",11, "contribution",12,"net_cont",12, 
                                     "Refugee",13, "Immigr",13, 
                                     "attach",14, "ideology",15, "direction",15, "avg_GDP_growth",16, 
                                     "Demands", .5, "Density", .5, "Intercept", 0.7, "^factor", 12), ncol = 2, byrow = T))
  colnames(ordering) <- c("varname", "position")
  
  sorting <- rep(NA, nrow(RES))
  for (i in 1:nrow(ordering)){
    pos <- grep(ordering$varname[i], RES$term)
    sorting[pos] <- i
  }
  
  RES <- RES[order(sorting),]
  RES <- RES[-grep("Source", RES$term), ]
  RES <- RES[-grep("Country_Code", RES$term), ]
  
  r2 <- round(c(summary(m1)$r.squared, summary(m2)$r.squared, summary(m3)$r.squared, summary(m4)$r.squared, summary(m5)$r.squared), 3)
  r2 <- c("R2", r2)
  obs <- c(nobs(m1), nobs(m2), nobs(m3), nobs(m4), nobs(m5))
  obs <- c("N", obs)
  
  RES <- rbind.data.frame(RES, obs, r2)
  
  tabl <- flextable(RES[,c(1:6)])
  tabl
}


##################################################################################################################################################################################


# Table 4: DV = Relative Densities


{
  m1 <- lm(scale(Density_hi-Density_lo) ~ 
             (scale(Ukip_vote_lag) + 
             scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Density_hi_lag-Density_lo_lag) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m1)
  m2 <- lm(scale(Density_hi-Density_lo) ~ 
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Density_hi_lag-Density_lo_lag) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m2)
  m3 <- lm(scale(Density_hi-Density_lo) ~ 
             scale(lag_XR_eur) +
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Density_hi_lag-Density_lo_lag) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m3)
  m4 <- lm(scale(Density_hi-Density_lo) ~ 
             scale(lag_XR_eur) +
             (scale(Ukip_vote_lag) + 
             scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Density_hi_lag-Density_lo_lag) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m4)
  m5 <- lm(scale(Density_hi-Density_lo) ~ 
             scale(lag_XR_eur) +
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Density_hi_lag-Density_lo_lag) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m5)
  
  all_models <- do.call(rbind, list(
    tidy(m1) %>% mutate(model = 1),
    tidy(m2) %>% mutate(model = 2),
    tidy(m3) %>% mutate(model = 3),
    tidy(m4) %>% mutate(model = 4),
    tidy(m5) %>% mutate(model = 5)))
  
  stars <- rep("", nrow(all_models))
  stars[all_models$p.value < .1] <- "^"
  stars[all_models$p.value < .05] <- "*"
  stars[all_models$p.value < .01] <- "**"
  stars[all_models$p.value < .001] <- "***"
  
  all_models <- tibble(cbind(all_models[,1:4], stars, all_models[,5:6]))
  all_models$stars <- as.character(all_models$stars)
  all_models$stars[is.na(all_models$stars)] <- ""
  
  ols_table <- all_models %>%
    select(-statistic, -p.value) %>% 
    gather(key, value, estimate:stars) %>%
    spread(model, value) 
  
  Table <- as.data.frame(ols_table[ols_table$key != "stars",])
  Stars <- as.data.frame(ols_table[ols_table$key == "stars",])
  
  tab <- Table
  for (i in 3:ncol(tab)) tab[,i] <- as.numeric(tab[,i])
  tab[,3:ncol(tab)] <- round(tab[,3:ncol(tab)], digits = 3)
  
  Est <- tab[tab$key == "estimate",]
  SE <- tab[tab$key == "std.error",]
  
  for (r in 1:nrow(Est)) {
    for(c in 3:ncol(Est)){
      if (!is.na(Est[r,c])) Est[r,c] <- paste0(Est[r,c], Stars[r, c])          
    }}  
  for (r in 1:nrow(SE)) {
    for(c in 3:ncol(SE)){
      if (!is.na(SE[r,c])) SE[r,c] <- paste0("(", SE[r, c], ")")          
    }}  
  
  RES <- Est
  for (r in 1:nrow(RES)) {
    for(c in 3:ncol(RES)){
      if (!is.na(RES[r,c])) RES[r,c] <- paste0(RES[r, c], "\n", SE[r, c])          
    }}  
  RES <- RES[,-which(colnames(RES) == "key")]
  
  ordering <- as.data.frame(matrix(c("X1",1, "X2",1, "XR",1,"Price",2, "Ukip",3, "Tories",4, "\\+", 5, "lag_eumemb",5, 
                                     "Nat_elect",6, "EP_elect",7,"vote_share",8, "cabinet",9, 
                                     "GDP_growth",10, "Unemployment",10, "rel_gdp",11, "contribution",12,"net_cont",12, 
                                     "Refugee",13, "Immigr",13, 
                                     "attach",14, "ideology",15, "direction",15, "avg_GDP_growth",16, 
                                     "Demands", .5, "Density", .5, "Intercept", 0.7, "^factor", 12), ncol = 2, byrow = T))
  colnames(ordering) <- c("varname", "position")
  
  sorting <- rep(NA, nrow(RES))
  for (i in 1:nrow(ordering)){
    pos <- grep(ordering$varname[i], RES$term)
    sorting[pos] <- i
  }
  
  RES <- RES[order(sorting),]
  RES <- RES[-grep("Source", RES$term), ]
  RES <- RES[-grep("Country_Code", RES$term), ]
  
  r2 <- round(c(summary(m1)$r.squared, summary(m2)$r.squared, summary(m3)$r.squared, summary(m4)$r.squared, summary(m5)$r.squared), 3)
  r2 <- c("R2", r2)
  obs <- c(nobs(m1), nobs(m2), nobs(m3), nobs(m4), nobs(m5))
  obs <- c("N", obs)
  
  RES <- rbind.data.frame(RES, obs, r2)
  
  tabl <- flextable(RES[,c(1:6)])
  tabl
}


#################################################################################################################################################################################


# Appendix B. Bond yields & bond spreads instead of XR data 

# Table B1: Yields 

{
  m1 <- lm(Demands ~ 
             (scale(Ukip_vote_lag) + 
                scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m1)
  m2 <- lm(Demands ~ 
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m2)
  m3 <- lm(Demands ~ 
             scale(lag_Price_uk) +
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m3)
  m4 <- lm(Demands ~ 
             scale(lag_Price_uk) +
             (scale(Ukip_vote_lag) + 
                scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m4)
  m5 <- lm(Demands ~ 
             scale(lag_Price_uk) +
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m5)
  
  all_models <- do.call(rbind, list(
    tidy(m1) %>% mutate(model = 1),
    tidy(m2) %>% mutate(model = 2),
    tidy(m3) %>% mutate(model = 3),
    tidy(m4) %>% mutate(model = 4),
    tidy(m5) %>% mutate(model = 5)))
  
  stars <- rep("", nrow(all_models))
  stars[all_models$p.value < .1] <- "^"
  stars[all_models$p.value < .05] <- "*"
  stars[all_models$p.value < .01] <- "**"
  stars[all_models$p.value < .001] <- "***"
  
  all_models <- tibble(cbind(all_models[,1:4], stars, all_models[,5:6]))
  all_models$stars <- as.character(all_models$stars)
  all_models$stars[is.na(all_models$stars)] <- ""
  
  ols_table <- all_models %>%
    select(-statistic, -p.value) %>% 
    gather(key, value, estimate:stars) %>%
    spread(model, value) 
  
  Table <- as.data.frame(ols_table[ols_table$key != "stars",])
  Stars <- as.data.frame(ols_table[ols_table$key == "stars",])
  
  tab <- Table
  for (i in 3:ncol(tab)) tab[,i] <- as.numeric(tab[,i])
  tab[,3:ncol(tab)] <- round(tab[,3:ncol(tab)], digits = 3)
  
  Est <- tab[tab$key == "estimate",]
  SE <- tab[tab$key == "std.error",]
  
  for (r in 1:nrow(Est)) {
    for(c in 3:ncol(Est)){
      if (!is.na(Est[r,c])) Est[r,c] <- paste0(Est[r,c], Stars[r, c])          
    }}  
  for (r in 1:nrow(SE)) {
    for(c in 3:ncol(SE)){
      if (!is.na(SE[r,c])) SE[r,c] <- paste0("(", SE[r, c], ")")          
    }}  
  
  RES <- Est
  for (r in 1:nrow(RES)) {
    for(c in 3:ncol(RES)){
      if (!is.na(RES[r,c])) RES[r,c] <- paste0(RES[r, c], "\n", SE[r, c])          
    }}  
  RES <- RES[,-which(colnames(RES) == "key")]
  
  ordering <- as.data.frame(matrix(c("X1",1, "X2",1, "XR",1,"Price",2, "Ukip",3, "Tories",4, "\\+", 5, "lag_eumemb",5, 
                                     "Nat_elect",6, "EP_elect",7,"vote_share",8, "cabinet",9, 
                                     "GDP_growth",10, "Unemployment",10, "rel_gdp",11, "contribution",12,"net_cont",12, 
                                     "Refugee",13, "Immigr",13, 
                                     "attach",14, "ideology",15, "direction",15, "avg_GDP_growth",16, 
                                     "Demands", .5, "Density", .5, "Intercept", 0.7, "^factor", 12), ncol = 2, byrow = T))
  colnames(ordering) <- c("varname", "position")
  
  sorting <- rep(NA, nrow(RES))
  for (i in 1:nrow(ordering)){
    pos <- grep(ordering$varname[i], RES$term)
    sorting[pos] <- i
  }
  
  RES <- RES[order(sorting),]
  RES <- RES[-grep("Source", RES$term), ]
  RES <- RES[-grep("Country_Code", RES$term), ]
  
  r2 <- round(c(summary(m1)$r.squared, summary(m2)$r.squared, summary(m3)$r.squared, summary(m4)$r.squared, summary(m5)$r.squared), 3)
  r2 <- c("R2", r2)
  obs <- c(nobs(m1), nobs(m2), nobs(m3), nobs(m4), nobs(m5))
  obs <- c("N", obs)
  
  RES <- rbind.data.frame(RES, obs, r2)
  
  tabl <- flextable(RES[,c(1:6)])
  tabl
}


# Table B2: Spreads

{
  m1 <- lm(Demands ~ 
             (scale(Ukip_vote_lag) + 
                scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m1)
  m2 <- lm(Demands ~ 
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m2)
  m3 <- lm(Demands ~ 
             scale(lag_Price_uk_mean) +
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m3)
  m4 <- lm(Demands ~ 
             scale(lag_Price_uk_mean) +
             (scale(Ukip_vote_lag) + 
                scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m4)
  m5 <- lm(Demands ~ 
             scale(lag_Price_uk_mean) +
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m5)
  
  all_models <- do.call(rbind, list(
    tidy(m1) %>% mutate(model = 1),
    tidy(m2) %>% mutate(model = 2),
    tidy(m3) %>% mutate(model = 3),
    tidy(m4) %>% mutate(model = 4),
    tidy(m5) %>% mutate(model = 5)))
  
  stars <- rep("", nrow(all_models))
  stars[all_models$p.value < .1] <- "^"
  stars[all_models$p.value < .05] <- "*"
  stars[all_models$p.value < .01] <- "**"
  stars[all_models$p.value < .001] <- "***"
  
  all_models <- tibble(cbind(all_models[,1:4], stars, all_models[,5:6]))
  all_models$stars <- as.character(all_models$stars)
  all_models$stars[is.na(all_models$stars)] <- ""
  
  ols_table <- all_models %>%
    select(-statistic, -p.value) %>% 
    gather(key, value, estimate:stars) %>%
    spread(model, value) 
  
  Table <- as.data.frame(ols_table[ols_table$key != "stars",])
  Stars <- as.data.frame(ols_table[ols_table$key == "stars",])
  
  tab <- Table
  for (i in 3:ncol(tab)) tab[,i] <- as.numeric(tab[,i])
  tab[,3:ncol(tab)] <- round(tab[,3:ncol(tab)], digits = 3)
  
  Est <- tab[tab$key == "estimate",]
  SE <- tab[tab$key == "std.error",]
  
  for (r in 1:nrow(Est)) {
    for(c in 3:ncol(Est)){
      if (!is.na(Est[r,c])) Est[r,c] <- paste0(Est[r,c], Stars[r, c])          
    }}  
  for (r in 1:nrow(SE)) {
    for(c in 3:ncol(SE)){
      if (!is.na(SE[r,c])) SE[r,c] <- paste0("(", SE[r, c], ")")          
    }}  
  
  RES <- Est
  for (r in 1:nrow(RES)) {
    for(c in 3:ncol(RES)){
      if (!is.na(RES[r,c])) RES[r,c] <- paste0(RES[r, c], "\n", SE[r, c])          
    }}  
  RES <- RES[,-which(colnames(RES) == "key")]
  
  ordering <- as.data.frame(matrix(c("X1",1, "X2",1, "XR",1,"Price",2, "Ukip",3, "Tories",4, "\\+", 5, "lag_eumemb",5, 
                                     "Nat_elect",6, "EP_elect",7,"vote_share",8, "cabinet",9, 
                                     "GDP_growth",10, "Unemployment",10, "rel_gdp",11, "contribution",12,"net_cont",12, 
                                     "Refugee",13, "Immigr",13, 
                                     "attach",14, "ideology",15, "direction",15, "avg_GDP_growth",16, 
                                     "Demands", .5, "Density", .5, "Intercept", 0.7, "^factor", 12), ncol = 2, byrow = T))
  colnames(ordering) <- c("varname", "position")
  
  sorting <- rep(NA, nrow(RES))
  for (i in 1:nrow(ordering)){
    pos <- grep(ordering$varname[i], RES$term)
    sorting[pos] <- i
  }
  
  RES <- RES[order(sorting),]
  RES <- RES[-grep("Source", RES$term), ]
  RES <- RES[-grep("Country_Code", RES$term), ]
  
  r2 <- round(c(summary(m1)$r.squared, summary(m2)$r.squared, summary(m3)$r.squared, summary(m4)$r.squared, summary(m5)$r.squared), 3)
  r2 <- c("R2", r2)
  obs <- c(nobs(m1), nobs(m2), nobs(m3), nobs(m4), nobs(m5))
  obs <- c("N", obs)
  
  RES <- rbind.data.frame(RES, obs, r2)
  
  tabl <- flextable(RES[,c(1:6)])
  tabl
}


##################################################################################################################################################################################


# Appendix C: Additional Robustness Chesks

# Table C1: Primary results incl. spatial lags

{
  m1 <- lm(Demands ~ 
             (scale(Ukip_vote_lag) + 
                scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(PO_spatial_lag) + scale(DV_spatial_lag) + 
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m1)
  m2 <- lm(Demands ~ 
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(PO_spatial_lag) + scale(DV_spatial_lag) + 
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m2)
  m3 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(PO_spatial_lag) + scale(DV_spatial_lag) + 
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m3)
  m4 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             (scale(Ukip_vote_lag) + 
                scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(PO_spatial_lag) + scale(DV_spatial_lag) + 
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m4)
  m5 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(PO_spatial_lag) + scale(DV_spatial_lag) + 
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = x)
  summary(m5)
  
  all_models <- do.call(rbind, list(
    tidy(m1) %>% mutate(model = 1),
    tidy(m2) %>% mutate(model = 2),
    tidy(m3) %>% mutate(model = 3),
    tidy(m4) %>% mutate(model = 4),
    tidy(m5) %>% mutate(model = 5)))
  
  stars <- rep("", nrow(all_models))
  stars[all_models$p.value < .1] <- "^"
  stars[all_models$p.value < .05] <- "*"
  stars[all_models$p.value < .01] <- "**"
  stars[all_models$p.value < .001] <- "***"
  
  all_models <- tibble(cbind(all_models[,1:4], stars, all_models[,5:6]))
  all_models$stars <- as.character(all_models$stars)
  all_models$stars[is.na(all_models$stars)] <- ""
  
  ols_table <- all_models %>%
    select(-statistic, -p.value) %>% 
    gather(key, value, estimate:stars) %>%
    spread(model, value) 
  
  Table <- as.data.frame(ols_table[ols_table$key != "stars",])
  Stars <- as.data.frame(ols_table[ols_table$key == "stars",])
  
  tab <- Table
  for (i in 3:ncol(tab)) tab[,i] <- as.numeric(tab[,i])
  tab[,3:ncol(tab)] <- round(tab[,3:ncol(tab)], digits = 3)
  
  Est <- tab[tab$key == "estimate",]
  SE <- tab[tab$key == "std.error",]
  
  for (r in 1:nrow(Est)) {
    for(c in 3:ncol(Est)){
      if (!is.na(Est[r,c])) Est[r,c] <- paste0(Est[r,c], Stars[r, c])          
    }}  
  for (r in 1:nrow(SE)) {
    for(c in 3:ncol(SE)){
      if (!is.na(SE[r,c])) SE[r,c] <- paste0("(", SE[r, c], ")")          
    }}  
  
  RES <- Est
  for (r in 1:nrow(RES)) {
    for(c in 3:ncol(RES)){
      if (!is.na(RES[r,c])) RES[r,c] <- paste0(RES[r, c], "\n", SE[r, c])          
    }}  
  RES <- RES[,-which(colnames(RES) == "key")]
  
  ordering <- as.data.frame(matrix(c("X1",1, "X2",1, "XR",1,"Price",2, "Ukip",3, "Tories",4, "\\+", 5, "lag_eumemb",5, 
                                     "Nat_elect",6, "EP_elect",7,"vote_share",8, "cabinet",9, 
                                     "GDP_growth",10, "Unemployment",10, "rel_gdp",11, "contribution",12,"net_cont",12, 
                                     "Refugee",13, "Immigr",13, 
                                     "attach",14, "ideology",15, "direction",15, "avg_GDP_growth",16, 
                                     "Demands", .5, "Density", .5, "Intercept", 0.7, "^factor", 12), ncol = 2, byrow = T))
  colnames(ordering) <- c("varname", "position")
  
  sorting <- rep(NA, nrow(RES))
  for (i in 1:nrow(ordering)){
    pos <- grep(ordering$varname[i], RES$term)
    sorting[pos] <- i
  }
  
  RES <- RES[order(sorting),]
  RES <- RES[-grep("Source", RES$term), ]
  RES <- RES[-grep("Country_Code", RES$term), ]
  
  r2 <- round(c(summary(m1)$r.squared, summary(m2)$r.squared, summary(m3)$r.squared, summary(m4)$r.squared, summary(m5)$r.squared), 3)
  r2 <- c("R2", r2)
  obs <- c(nobs(m1), nobs(m2), nobs(m3), nobs(m4), nobs(m5))
  obs <- c("N", obs)
  
  RES <- rbind.data.frame(RES, obs, r2)
  
  tabl <- flextable(RES[,c(1:6)])
  tabl
}


# Table C2: Primary results excl. ESP & IRL

{
  
  xs <- x
  xs <- xs[xs$Country_Code != "ESP",]
  xs <- xs[xs$Country_Code != "IRL",]
  
  m1 <- lm(Demands ~ 
             (scale(Ukip_vote_lag) + 
                scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = xs)
  summary(m1)
  m2 <- lm(Demands ~ 
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = xs)
  summary(m2)
  m3 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = xs)
  summary(m3)
  m4 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             (scale(Ukip_vote_lag) + 
                scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = xs)
  summary(m4)
  m5 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = xs)
  summary(m5)
  
  all_models <- do.call(rbind, list(
    tidy(m1) %>% mutate(model = 1),
    tidy(m2) %>% mutate(model = 2),
    tidy(m3) %>% mutate(model = 3),
    tidy(m4) %>% mutate(model = 4),
    tidy(m5) %>% mutate(model = 5)))
  
  stars <- rep("", nrow(all_models))
  stars[all_models$p.value < .1] <- "^"
  stars[all_models$p.value < .05] <- "*"
  stars[all_models$p.value < .01] <- "**"
  stars[all_models$p.value < .001] <- "***"
  
  all_models <- tibble(cbind(all_models[,1:4], stars, all_models[,5:6]))
  all_models$stars <- as.character(all_models$stars)
  all_models$stars[is.na(all_models$stars)] <- ""
  
  ols_table <- all_models %>%
    select(-statistic, -p.value) %>% 
    gather(key, value, estimate:stars) %>%
    spread(model, value) 
  
  Table <- as.data.frame(ols_table[ols_table$key != "stars",])
  Stars <- as.data.frame(ols_table[ols_table$key == "stars",])
  
  tab <- Table
  for (i in 3:ncol(tab)) tab[,i] <- as.numeric(tab[,i])
  tab[,3:ncol(tab)] <- round(tab[,3:ncol(tab)], digits = 3)
  
  Est <- tab[tab$key == "estimate",]
  SE <- tab[tab$key == "std.error",]
  
  for (r in 1:nrow(Est)) {
    for(c in 3:ncol(Est)){
      if (!is.na(Est[r,c])) Est[r,c] <- paste0(Est[r,c], Stars[r, c])          
    }}  
  for (r in 1:nrow(SE)) {
    for(c in 3:ncol(SE)){
      if (!is.na(SE[r,c])) SE[r,c] <- paste0("(", SE[r, c], ")")          
    }}  
  
  RES <- Est
  for (r in 1:nrow(RES)) {
    for(c in 3:ncol(RES)){
      if (!is.na(RES[r,c])) RES[r,c] <- paste0(RES[r, c], "\n", SE[r, c])          
    }}  
  RES <- RES[,-which(colnames(RES) == "key")]
  
  ordering <- as.data.frame(matrix(c("X1",1, "X2",1, "XR",1,"Price",2, "Ukip",3, "Tories",4, "\\+", 5, "lag_eumemb",5, 
                                     "Nat_elect",6, "EP_elect",7,"vote_share",8, "cabinet",9, 
                                     "GDP_growth",10, "Unemployment",10, "rel_gdp",11, "contribution",12,"net_cont",12, 
                                     "Refugee",13, "Immigr",13, 
                                     "attach",14, "ideology",15, "direction",15, "avg_GDP_growth",16, 
                                     "Demands", .5, "Density", .5, "Intercept", 0.7, "^factor", 12), ncol = 2, byrow = T))
  colnames(ordering) <- c("varname", "position")
  
  sorting <- rep(NA, nrow(RES))
  for (i in 1:nrow(ordering)){
    pos <- grep(ordering$varname[i], RES$term)
    sorting[pos] <- i
  }
  
  RES <- RES[order(sorting),]
  RES <- RES[-grep("Source", RES$term), ]
  RES <- RES[-grep("Country_Code", RES$term), ]
  
  r2 <- round(c(summary(m1)$r.squared, summary(m2)$r.squared, summary(m3)$r.squared, summary(m4)$r.squared, summary(m5)$r.squared), 3)
  r2 <- c("R2", r2)
  obs <- c(nobs(m1), nobs(m2), nobs(m3), nobs(m4), nobs(m5))
  obs <- c("N", obs)
  
  RES <- rbind.data.frame(RES, obs, r2)
  
  tabl <- flextable(RES[,c(1:6)])
  tabl
}


##################################################################################################################################################################################


# Appendix D: Split-sample analysis - soft vs. hard euroskeptics:

p <- read.table("ParlGov_Euroskepticism.txt", stringsAsFactors = F, sep = "\t", header = T)
p <- p[,c(1,12)]
colnames(p)[1] <- "Country_Code"
p$hard <- rep(0, nrow(p))
p$hard[p$eu_anti_pro < 2] <- 1

p <- p[order(p$eu_anti_pro),]
rownames(p) <- NULL

y <- merge(x, p, all.x = T)


for (j in 0:1) {
  
  m1 <- lm(Demands ~ 
             (scale(Ukip_vote_lag) + 
                scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = y[y$hard == j,])
  summary(m1)
  m2 <- lm(Demands ~ 
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = y[y$hard == j,])
  summary(m2)
  m3 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = y[y$hard == j,])
  summary(m3)
  m4 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             (scale(Ukip_vote_lag) + 
                scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = y[y$hard == j,])
  summary(m4)
  m5 <- lm(Demands ~ 
             scale(lag_XR_eur) +
             I(scale(Ukip_vote_lag) +
                 scale(Tories_vote_lag)) + 
             scale(lag_eumembgood) +
             EP_elect + Nat_elect + 
             scale(vote_share) + cabinet_party + I(rel_gdp*1000) +
             scale(net.contribution.per.capita) + 
             scale(log(lag_Refugees_pop_shr)) +
             scale(log(lag_Immigration_pop_shr)) +
             scale(lag_attachCountry_EU) + scale(lag_directionNat) +
             scale(GDP_growth) + scale(avg_GDP_growth-GDP_growth) +
             scale(Demands_lag_1q) + scale(Year) *
             factor(Country_Code) + factor(Source), data = y[y$hard == j,])
  summary(m5)
  
  all_models <- do.call(rbind, list(
    tidy(m1) %>% mutate(model = 1),
    tidy(m2) %>% mutate(model = 2),
    tidy(m3) %>% mutate(model = 3),
    tidy(m4) %>% mutate(model = 4),
    tidy(m5) %>% mutate(model = 5)))
  
  stars <- rep("", nrow(all_models))
  stars[all_models$p.value < .1] <- "^"
  stars[all_models$p.value < .05] <- "*"
  stars[all_models$p.value < .01] <- "**"
  stars[all_models$p.value < .001] <- "***"
  
  all_models <- tibble(cbind(all_models[,1:4], stars, all_models[,5:6]))
  all_models$stars <- as.character(all_models$stars)
  all_models$stars[is.na(all_models$stars)] <- ""
  
  ols_table <- all_models %>%
    select(-statistic, -p.value) %>% 
    gather(key, value, estimate:stars) %>%
    spread(model, value) 
  
  Table <- as.data.frame(ols_table[ols_table$key != "stars",])
  Stars <- as.data.frame(ols_table[ols_table$key == "stars",])
  
  tab <- Table
  for (i in 3:ncol(tab)) tab[,i] <- as.numeric(tab[,i])
  tab[,3:ncol(tab)] <- round(tab[,3:ncol(tab)], digits = 3)
  
  Est <- tab[tab$key == "estimate",]
  SE <- tab[tab$key == "std.error",]
  
  for (r in 1:nrow(Est)) {
    for(c in 3:ncol(Est)){
      if (!is.na(Est[r,c])) Est[r,c] <- paste0(Est[r,c], Stars[r, c])          
    }}  
  for (r in 1:nrow(SE)) {
    for(c in 3:ncol(SE)){
      if (!is.na(SE[r,c])) SE[r,c] <- paste0("(", SE[r, c], ")")          
    }}  
  
  RES <- Est
  for (r in 1:nrow(RES)) {
    for(c in 3:ncol(RES)){
      if (!is.na(RES[r,c])) RES[r,c] <- paste0(RES[r, c], "\n", SE[r, c])          
    }}  
  RES <- RES[,-which(colnames(RES) == "key")]
  
  ordering <- as.data.frame(matrix(c("X1",1, "X2",1, "XR",1,"Price",2, "Ukip",3, "Tories",4, "\\+", 5, "lag_eumemb",5, 
                                     "Nat_elect",6, "EP_elect",7,"vote_share",8, "cabinet",9, 
                                     "GDP_growth",10, "Unemployment",10, "rel_gdp",11, "contribution",12,"net_cont",12, 
                                     "Refugee",13, "Immigr",13, 
                                     "attach",14, "ideology",15, "direction",15, "avg_GDP_growth",16, 
                                     "Demands", .5, "Density", .5, "Intercept", 0.7, "^factor", 12), ncol = 2, byrow = T))
  colnames(ordering) <- c("varname", "position")
  
  sorting <- rep(NA, nrow(RES))
  for (i in 1:nrow(ordering)){
    pos <- grep(ordering$varname[i], RES$term)
    sorting[pos] <- i
  }
  
  RES <- RES[order(sorting),]
  RES <- RES[-grep("Source", RES$term), ]
  RES <- RES[-grep("Country_Code", RES$term), ]
  
  r2 <- round(c(summary(m1)$r.squared, summary(m2)$r.squared, summary(m3)$r.squared, summary(m4)$r.squared, summary(m5)$r.squared), 3)
  r2 <- c("R2", r2)
  obs <- c(nobs(m1), nobs(m2), nobs(m3), nobs(m4), nobs(m5))
  obs <- c("N", obs)
  
  RES <- rbind.data.frame(RES, obs, r2)
  if (j == 0) names(RES)[1] <- "Soft"
  if (j == 1) names(RES)[1] <- "Hard"
  
  tabl <- flextable(RES[,c(1:6)])
  print(tabl)
}


# R and package Versions:

# sessionInfo()
# R version 3.6.1 (2019-07-05)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 19044)
# 
# Matrix products: default
# 
# locale:
# [1] LC_COLLATE=English_Switzerland.1252  LC_CTYPE=English_Switzerland.1252   
# [3] LC_MONETARY=English_Switzerland.1252 LC_NUMERIC=C                        
# [5] LC_TIME=English_Switzerland.1252    
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] flextable_0.6.1 broom_0.5.6     forcats_0.4.0   stringr_1.4.0   dplyr_1.0.0     purrr_0.3.4    
# [7] readr_1.3.1     tidyr_1.1.2     tibble_3.0.1    tidyverse_1.2.1 magrittr_1.5    zoo_1.8-6      
# [13] ggplot2_3.3.2  
# 
# loaded via a namespace (and not attached):
# [1] tidyselect_1.1.0  xfun_0.15         haven_2.3.1       lattice_0.20-45   colorspace_1.4-1 
# [6] vctrs_0.3.1       generics_0.1.0    htmltools_0.5.1.1 base64enc_0.1-3   rlang_0.4.6      
# [11] pillar_1.4.4      glue_1.4.1        withr_2.1.2       gdtools_0.2.2     modelr_0.1.5     
# [16] readxl_1.3.1      uuid_0.1-4        lifecycle_0.2.0   munsell_0.5.0     gtable_0.3.0     
# [21] cellranger_1.1.0  rvest_0.3.5       zip_2.1.1         evaluate_0.14     knitr_1.28       
# [26] fansi_0.4.0       Rcpp_1.0.2        backports_1.1.5   scales_1.0.0      jsonlite_1.6.1   
# [31] systemfonts_0.3.2 hms_0.5.1         digest_0.6.25     stringi_1.4.3     grid_3.6.1       
# [36] cli_2.0.2         tools_3.6.1       crayon_1.3.4      pkgconfig_2.0.3   ellipsis_0.3.0   
# [41] data.table_1.13.6 xml2_1.2.2        lubridate_1.7.4   assertthat_0.2.1  rmarkdown_2.1    
# [46] httr_1.4.1        officer_0.3.16    rstudioapi_0.11   R6_2.4.0          nlme_3.1-140     
# [51] compiler_3.6.1  